// 查
var express = require("express");
var dayjs = require("dayjs");
var router = express.Router("dayjs");
const db = require("../disposition/db");
// 查询统计接口——————————————————————————————————————————————————————————————————————
router.get("/statistics", (req, res) => {
  console.log("查询统计接口");
  let state = {
    // 审核角色
    role: 0,
    // 幻灯镜
    mirror: 0,
    // 活动
    activity: 0,
    // 卡池
    Cardpool: 0,
    // 调伏战
    relic: 0,
    // 道具
    cuttingtool: 0,
    // 封灭战
    warOfAnnihilation:0
  };
  // 查询角色审核表
  const sql = "SELECT * FROM character_guide WHERE Agree=0";
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 查找为未审核的数量
    state.role = result.length;
    // 查询幻灯镜审核表
    const sql = "SELECT * FROM slide_projector WHERE Agree=0";
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 查找为幻灯镜未审核的数量
      state.mirror = result.length;
      // 查活动审核表
      const sql = "SELECT * FROM activity WHERE Agree=0";
      db.query(sql, (err, result) => {
        if (err) {
          return;
        }
        // 查找为活动未审核的数量
        state.activity = result.length;
        // 查道具审核表
        const sql = "SELECT * FROM props WHERE Agree=0";
        db.query(sql, (err, result) => {
          if (err) {
            return;
          }
          // 查找为活动未审核的数量
          state.cuttingtool = result.length;
          // 查调伏战审核表
          const sql = "SELECT * FROM ambush_warfare WHERE Agree=0";
          db.query(sql, (err, result) => {
            if (err) {
              return;
            }
            // 查找为活动未审核的数量
            state.relic = result.length;
            // res.send({
            //   data: state,
            //   success: true,
            // });
            const sql = "SELECT * FROM activity WHERE Agree=0";
            db.query(sql, (err, result) => {
              if (err) {
                return;
              }
              // 查找为活动未审核的数量
              state.activity = result.length;
              // 查道具审核表
              const sql = "SELECT * FROM props WHERE Agree=0";
              db.query(sql, (err, result) => {
                if (err) {
                  return;
                }
                // 查找为活动未审核的数量
                state.cuttingtool = result.length;
                // 查卡池审核表
                const sql = "SELECT * FROM card_pools WHERE Agree=0";
                db.query(sql, (err, result) => {
                  if (err) {
                    return;
                  }
                  // 查找卡池未审核的数量
                  state.Cardpool = result.length;
                            // 查卡池审核表
                const sql = "SELECT * FROM war_of_annihilation WHERE Agree=0";
                db.query(sql, (err, result) => {
                  if (err) {
                    return;
                  }
                  // 查找封灭战未审核的数量
                  state.warOfAnnihilation = result.length;
                  res.send({
                    data: state,
                    success: true,
                  });
                });
                });
              });
            });
          });
        });
      });
    });
  });
});
// 查询角色审核接口——————————————————————————————————————————————————————————————————
router.get("/role", (req, res) => {
  console.log("查询角色审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM character_guide WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  character_guide WHERE Agree=0 AND Created_ID=${req.auth.id} ORDER BY id desc`)
      : (sql = `SELECT * FROM character_guide WHERE Agree=0 AND Created_ID=${req.auth.id} ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.Attack_radius = JSON.parse(item.Attack_radius);
      item.Batter1 = JSON.parse(item.Batter1);
      item.Batter1_content = JSON.parse(item.Batter1_content);
      item.Batter2 = JSON.parse(item.Batter2);
      item.Batter2_content = JSON.parse(item.Batter2_content);
      item.Batter3 = JSON.parse(item.Batter3);
      item.Batter3_content = JSON.parse(item.Batter3_content);
      item.Deed_materials = JSON.parse(item.Deed_materials);
      item.arousal = JSON.parse(item.arousal);
      item.attribute = JSON.parse(item.attribute);
      item.characteristic1_content = JSON.parse(item.characteristic1_content);
      item.characteristic2_content = JSON.parse(item.characteristic2_content);
      !item.Summons ? "" : (item.Summons = JSON.parse(item.Summons));
      item.task = JSON.parse(item.task);
      let GetTime = new Date(item.Created_Data).getTime();
      item.Created_Data = new Date(GetTime + 86400000);
      // item.Created_Data = item.Created_Data.split("T")[0];
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查询角色修改接口——————————————————————————————————————————————————————————————————————
router.get("/revise", (req, res) => {
  console.log("查询角色修改接口");
  // 查询角色表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库

    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from character_guide WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = ` SELECT * from character_guide WHERE name LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 OR alias LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from character_guide WHERE Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from character_guide WHERE name LIKE '%${req.query.Search}%' AND Agree=1 OR alias LIKE '%${req.query.Search}%' AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.Attack_radius = JSON.parse(JSON.stringify(item.Attack_radius));
      item.Batter1 = JSON.parse(JSON.stringify(item.Batter1));
      item.Batter1_content = JSON.parse(JSON.stringify(item.Batter1_content));
      item.Batter2 = JSON.parse(JSON.stringify(item.Batter2));
      item.Batter2_content = JSON.parse(JSON.stringify(item.Batter2_content));
      item.Batter3 = JSON.parse(JSON.stringify(item.Batter3));
      item.Batter3_content = JSON.parse(JSON.stringify(item.Batter3_content));
      item.Deed_materials = JSON.parse(JSON.stringify(item.Deed_materials));
      item.arousal = JSON.parse(JSON.stringify(item.arousal));
      item.attribute = JSON.parse(JSON.stringify(item.attribute));
      item.characteristic1_content = JSON.parse(
        JSON.stringify(item.characteristic1_content)
      );
      !item.Summons ? "" : (item.Summons = JSON.parse(item.Summons));
      let GetTime = new Date(item.Created_Data).getTime();
      item.Created_Data = new Date(GetTime + 86400000);
      // item.Created_Data = item.Created_Data.split("T")[0];
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细角色接口——————————————————————————————————————————————————————————————————
router.get("/RoleVerbose", (req, res) => {
  console.log("查看详细角色接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from character_guide  WHERE  id=${req.query.id}`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from character_guide WHERE id=${req.query.id}`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      //将一些数据转化为js数据
      result.some((item) => {
        item.Attack_radius = JSON.parse(item.Attack_radius);
        item.Batter1 = JSON.parse(item.Batter1);
        item.Batter1_content = JSON.parse(item.Batter1_content);
        item.Batter2 = JSON.parse(item.Batter2);
        item.Batter2_content = JSON.parse(item.Batter2_content);
        item.Batter3 = JSON.parse(item.Batter3);
        item.Batter3_content = JSON.parse(item.Batter3_content);
        item.Deed_materials = JSON.parse(item.Deed_materials);
        item.arousal = JSON.parse(item.arousal);
        item.attribute = JSON.parse(item.attribute);
        item.characteristic1_content = JSON.parse(item.characteristic1_content);
        item.characteristic2_content = JSON.parse(item.characteristic2_content);
        item.task = JSON.parse(item.task);
        !item.Summons ? "" : (item.Summons = JSON.parse(item.Summons));
        let GetTime = new Date(item.Created_Data).getTime();
        item.Created_Data = new Date(GetTime + 86400000);
        // item.Created_Data = item.Created_Data.split("T")[0];
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查询灯幻镜审核接口——————————————————————————————————————————————————————————————————
router.get("/mirror", (req, res) => {
  console.log("查询灯幻镜审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM slide_projector WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  slide_projector WHERE Created_ID=${req.auth.id}  AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * FROM slide_projector WHERE Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.effect = JSON.parse(item.effect);
      let GetTime = new Date(item.Created_Data).getTime();
      item.Created_Data = new Date(GetTime + 86400000);
      // item.Created_Data = item.Created_Data.split("T")[0];
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查询灯幻镜修改接口——————————————————————————————————————————————————————————————————————
router.get("/ModifyTheMirror", (req, res) => {
  console.log("查询灯幻镜修改接口");
  // 查询幻灯镜表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from slide_projector WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * from slide_projector WHERE name LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 OR Artist LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 OR alias LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from slide_projector WHERE  Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from slide_projector WHERE name LIKE '%${req.query.Search}%' AND Agree=1 OR Artist LIKE '%${req.query.Search}%' AND Agree=1 OR alias LIKE '%${req.query.Search}%' AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    // 灯幻镜数据转化
    result.some((item) => {
      item.effect = JSON.parse(item.effect);
      let GetTime = new Date(item.Created_Data).getTime();
      item.Created_Data = new Date(GetTime + 86400000);
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细灯幻镜接口——————————————————————————————————————————————————————————————————
router.get("/TheLampMirrorIsDetailed", (req, res) => {
  console.log("查看详细灯幻镜接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from slide_projector WHERE id=${req.query.id} AND Agree=0`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from slide_projector WHERE id=${req.query.id} AND Agree=1`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 灯幻镜转化
      result.some((item) => {
        item.effect = JSON.parse(item.effect);
        let GetTime = new Date(item.Created_Data).getTime();
        item.Created_Data = new Date(GetTime + 86400000);
        // item.Created_Data = item.Created_Data.split("T")[0];
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查询活动审核接口——————————————————————————————————————————————————————————————————
router.get("/Activity", (req, res) => {
  console.log("查询活动审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM activity WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  activity WHERE Created_ID=${req.auth.id}  AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * FROM activity WHERE Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      let GetTime = new Date(item.Created_Data).getTime();
      item.Created_Data !== null
        ? (item.Created_Data = new Date(GetTime + 86400000))
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 日期
        let BeginDate = `${Begin.getFullYear()}-${
          Begin.getMonth() + 1 < 10
            ? `0${Begin.getMonth() + 1}`
            : Begin.getMonth() + 1
        }-${Begin.getDate() < 10 ? `0${Begin.getDate()}` : Begin.getDate()}`;
        //  时间
        let BeginTime = `${
          Begin.getHours() < 10 ? `0${Begin.getHours()}` : Begin.getHours()
        }:${
          Begin.getMinutes() < 10
            ? `0${Begin.getMinutes()}`
            : Begin.getMinutes()
        }:${
          Begin.getSeconds() < 10
            ? `0${Begin.getSeconds()}`
            : Begin.getSeconds()
        }`;
        // 赋值开始时间
        item.Start_time = BeginDate + " " + BeginTime;
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        let Begin2 = new Date(item.End_time);
        // 日期
        let BeginDate2 = `${Begin2.getFullYear()}-${
          Begin2.getMonth() + 1 < 10
            ? `0${Begin2.getMonth() + 1}`
            : Begin2.getMonth() + 1
        }-${Begin2.getDate() < 10 ? `0${Begin2.getDate()}` : Begin2.getDate()}`;
        //  时间
        let BeginTime2 = `${
          Begin2.getHours() < 10 ? `0${Begin2.getHours()}` : Begin2.getHours()
        }:${
          Begin2.getMinutes() < 10
            ? `0${Begin2.getMinutes()}`
            : Begin2.getMinutes()
        }:${
          Begin2.getSeconds() < 10
            ? `0${Begin2.getSeconds()}`
            : Begin2.getSeconds()
        }`;
        // 赋值结束时间
        item.End_time = BeginDate2 + " " + BeginTime2;
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查询活动修改接口——————————————————————————————————————————————————————————————————————
router.get("/ModifyTheActivity", (req, res) => {
  console.log("查询活动修改接口");
  // 查询幻灯镜表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from activity WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * from activity WHERE name LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from activity WHERE  Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from activity WHERE name LIKE '%${req.query.Search}%'AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      let GetTime = new Date(item.Created_Data).getTime();
      item.Created_Data !== null
        ? (item.Created_Data = new Date(GetTime + 86400000))
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 日期
        let BeginDate = `${Begin.getFullYear()}-${
          Begin.getMonth() + 1 < 10
            ? `0${Begin.getMonth() + 1}`
            : Begin.getMonth() + 1
        }-${Begin.getDate() < 10 ? `0${Begin.getDate()}` : Begin.getDate()}`;
        //  时间
        let BeginTime = `${
          Begin.getHours() < 10 ? `0${Begin.getHours()}` : Begin.getHours()
        }:${
          Begin.getMinutes() < 10
            ? `0${Begin.getMinutes()}`
            : Begin.getMinutes()
        }:${
          Begin.getSeconds() < 10
            ? `0${Begin.getSeconds()}`
            : Begin.getSeconds()
        }`;
        // 赋值开始时间
        item.Start_time = BeginDate + " " + BeginTime;
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        let Begin2 = new Date(item.End_time);
        // 日期
        let BeginDate2 = `${Begin2.getFullYear()}-${
          Begin2.getMonth() + 1 < 10
            ? `0${Begin2.getMonth() + 1}`
            : Begin2.getMonth() + 1
        }-${Begin2.getDate() < 10 ? `0${Begin2.getDate()}` : Begin2.getDate()}`;
        //  时间
        let BeginTime2 = `${
          Begin2.getHours() < 10 ? `0${Begin2.getHours()}` : Begin2.getHours()
        }:${
          Begin2.getMinutes() < 10
            ? `0${Begin2.getMinutes()}`
            : Begin2.getMinutes()
        }:${
          Begin2.getSeconds() < 10
            ? `0${Begin2.getSeconds()}`
            : Begin2.getSeconds()
        }`;
        // 赋值结束时间
        item.End_time = BeginDate2 + " " + BeginTime2;
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细活动接口——————————————————————————————————————————————————————————————————
router.get("/ActivityDetails", (req, res) => {
  console.log("查看详细活动接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from activity WHERE id=${req.query.id} AND Agree=0`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from activity WHERE id=${req.query.id} AND Agree=1`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 活动转化
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        let GetTime = new Date(item.Created_Data).getTime();
        item.Created_Data !== null
          ? (item.Created_Data = new Date(GetTime + 86400000))
          : "";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          let Begin = new Date(item.Start_time);
          // 日期
          let BeginDate = `${Begin.getFullYear()}-${
            Begin.getMonth() + 1 < 10
              ? `0${Begin.getMonth() + 1}`
              : Begin.getMonth() + 1
          }-${Begin.getDate() < 10 ? `0${Begin.getDate()}` : Begin.getDate()}`;
          //  时间
          let BeginTime = `${
            Begin.getHours() < 10 ? `0${Begin.getHours()}` : Begin.getHours()
          }:${
            Begin.getMinutes() < 10
              ? `0${Begin.getMinutes()}`
              : Begin.getMinutes()
          }:${
            Begin.getSeconds() < 10
              ? `0${Begin.getSeconds()}`
              : Begin.getSeconds()
          }`;
          // 赋值开始时间
          item.Start_time = BeginDate + " " + BeginTime;
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          let Begin2 = new Date(item.End_time);
          // 日期
          let BeginDate2 = `${Begin2.getFullYear()}-${
            Begin2.getMonth() + 1 < 10
              ? `0${Begin2.getMonth() + 1}`
              : Begin2.getMonth() + 1
          }-${
            Begin2.getDate() < 10 ? `0${Begin2.getDate()}` : Begin2.getDate()
          }`;
          //  时间
          let BeginTime2 = `${
            Begin2.getHours() < 10 ? `0${Begin2.getHours()}` : Begin2.getHours()
          }:${
            Begin2.getMinutes() < 10
              ? `0${Begin2.getMinutes()}`
              : Begin2.getMinutes()
          }:${
            Begin2.getSeconds() < 10
              ? `0${Begin2.getSeconds()}`
              : Begin2.getSeconds()
          }`;
          // 赋值结束时间
          item.End_time = BeginDate2 + " " + BeginTime2;
        }
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查询道具审核接口——————————————————————————————————————————————————————————————————
router.get("/Prop", (req, res) => {
  console.log("查询道具审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM props WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  props WHERE Created_ID=${req.auth.id}  AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * FROM props WHERE Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      !item.Created_Data
        ? ""
        : (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ));
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查询道具修改接口——————————————————————————————————————————————————————————————————————
router.get("/ModifyProp", (req, res) => {
  console.log("查询道具修改接口");
  // 查询幻灯镜表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from props WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * from props WHERE name LIKE '%${req.query.Search}%' AND Agree=0 AND Created_ID=${req.auth.id} OR classify LIKE '%${req.query.Search}%' AND Agree=0 AND Created_ID=${req.auth.id} ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from props WHERE  Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from props WHERE name LIKE '%${req.query.Search}%' AND Agree=1 OR classify LIKE '%${req.query.Search}%' AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    // 活动转化
    //将一些数据转化为js数据
    result.some((item) => {
      !item.Created_Data
        ? ""
        : (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ));
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细道具接口——————————————————————————————————————————————————————————————————
router.get("/DetailedProps", (req, res) => {
  console.log("查看详细道具接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from props WHERE id=${req.query.id} AND Agree=0`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from props WHERE id=${req.query.id} AND Agree=1`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查询调伏战审核接口——————————————————————————————————————————————————————————————————
router.get("/AmbushWarfare", (req, res) => {
  console.log("查询调伏战审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM ambush_warfare WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  ambush_warfare WHERE Created_ID=${req.auth.id}  AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * FROM ambush_warfare WHERE Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
      item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
      item.Strategy = JSON.parse(item.Strategy);
      item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
      item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item.Created_Data !== null
        ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查询调伏战修改接口——————————————————————————————————————————————————————————————————————
router.get("/ModifiedAmbushWarfare", (req, res) => {
  console.log("查询调伏战修改接口");
  // 查询幻灯镜表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from ambush_warfare WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * from ambush_warfare WHERE name LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from ambush_warfare WHERE  Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from ambush_warfare WHERE name LIKE '%${req.query.Search}%'AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
      item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
      item.Strategy = JSON.parse(item.Strategy);
      item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
      item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item.Created_Data !== null
        ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细调伏战接口——————————————————————————————————————————————————————————————————
router.get("/ActivityDetailSget", (req, res) => {
  console.log("查看详细调伏战接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from ambush_warfare WHERE id=${req.query.id} AND Agree=0`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from ambush_warfare WHERE id=${req.query.id} AND Agree=1`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 调伏战转化
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item.Created_Data !== null
          ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
          : "";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查询卡池审核接口——————————————————————————————————————————————————————————————————
router.get("/CardPoolAudit", (req, res) => {
  console.log("查询卡池审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM card_pools WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  card_pools WHERE Created_ID=${req.auth.id}  AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * FROM card_pools WHERE Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查询卡池修改接口——————————————————————————————————————————————————————————————————————
router.get("/CardPoolModification", (req, res) => {
  console.log("查询卡池修改接口");
  // 查询幻灯镜表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from card_pools WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * from card_pools WHERE name LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from card_pools WHERE  Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from card_pools WHERE name LIKE '%${req.query.Search}%'AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    result.some((item) => {
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Created_Data !== null
        ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细调伏战卡池接口——————————————————————————————————————————————————————————————————
router.get("/TheCardPoolIsDetailed", (req, res) => {
  console.log("查看详细卡池接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from card_pools WHERE id=${req.query.id} AND Agree=0`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from card_pools WHERE id=${req.query.id} AND Agree=1`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 调伏战转化
      result.some((item) => {
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Created_Data !== null
          ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
          : "";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查看抽卡模拟器接口——————————————————————————————————————————————————————————————————
router.get("/list", (req, res) => {
  console.log("查看抽卡模拟器");
  // 查询角色表
  let sql = "";

  if (
    req.query.state === "0" ||
    req.query.state === "1" ||
    req.query.state === "2" ||
    req.query.state === "3"
  ) {
    sql = `SELECT * from leaderboard WHERE id="${req.query.state}"`;
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }

  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 调伏战转化
      result.some((item) => {
        item.content = JSON.parse(item.content);
        item.AgreeData !== null
          ? (item.AgreeData = dayjs(item.AgreeData + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            ))
          : "";
      });
      res.json({
        data: result[0],
        success: true,
      });
    }
  });
});
// 查看抽卡模拟器卡池接口——————————————————————————————————————————————————————————————————
router.get("/EmulateACardPool", (req, res) => {
  console.log("查看抽卡模拟器卡池接口");
  // 查询角色表
  let sql = "";

  if (req.query.state === "进行中") {
    sql = `SELECT * from simulated_gacha WHERE End_time > "${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}" ORDER BY id desc`;
  } else if (req.query.state === "已结束") {
    sql = `SELECT * from simulated_gacha WHERE End_time < "${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}" ORDER BY id desc`;
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }

  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    // 调伏战转化
    result.some((item) => {
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Start_time !== null
        ? (item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          ))
        : "";
      item.End_time !== null
        ? (item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          ))
        : "";
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 获取遗迹导入层数数据接口——————————————————————————————————————————————————————————————————
router.get("/ImportTheRuins", (req, res) => {
  console.log("获取遗迹导入层数数据接口");
  // 查询表
  if (req.query.id) {
    let sql = `SELECT * from relic WHERE nameID=${req.query.id}`;
    // 查找数据库
    db.query(sql, function (err, result) {
      if (err) {
        return;
      }
      let Res = [];
      // 赋值转化
      result.some((item) => {
        Res.push({
          id: item.id,
          name: item.Level,
        });
      });
      res.json({
        data: Res,
        success: true,
      });
    });
  } else {
    res.send({
      msg: "获取遗迹导入层数数据失败",
      success: false,
    });
  }
});
// 导入对应遗迹层级接口——————————————————————————————————————————————————————————————————
router.get("/TheRuinsAreDetailed", (req, res) => {
  console.log("获取遗迹导入层数数据接口");
  // 查询表
  if (req.query.id) {
    let sql = `SELECT * from relic WHERE id=${req.query.id}`;
    // 查找数据库
    db.query(sql, function (err, result) {
      if (err) {
        return;
      }
      if (result.length !== 0) {
        result.some((item) => {
          item.after1 = JSON.parse(item.after1);
          item.after2 = JSON.parse(item.after2);
          item.after3 = JSON.parse(item.after3);
          item.former1 = JSON.parse(item.former1);
          item.former2 = JSON.parse(item.former2);
          item.former3 = JSON.parse(item.former3);
          !item.Created_Data
            ? ""
            : (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
                "YYYY-MM-DD"
              ));
        });
        res.send({
          data: result[0],
          success: true,
        });
      } else {
        res.send({
          msg: "未找到对应数据",
          success: false,
        });
      }
    });
  } else {
  }
});
//获取遗迹编辑数据接口——————————————————————————————————————————————————————————————————
router.get("/RelicsEditData", (req, res) => {
  console.log("获取遗迹编辑数据");
  // 查询角色表
  let sql = "";
  let row = [];
  if (!req.query.name) {
    sql = `SELECT * from enum WHERE classify='遗迹' ORDER BY id desc`;
  } else {
    sql = `SELECT * from enum WHERE classify='遗迹' AND name LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
      item["content"] = [];
      // 一层赋值给输出字段
      row.push(item);
    });
    let sql2 = `SELECT * from relic`;
    // 查二层
    db.query(sql2, function (err, result2) {
      // 把对应二层赋值给对应一层内容字段中去
      // 循环一层
      row.some((item) => {
        // 循环二层数据并重新赋值日期
        result2.some((item3) => {
          item3.Created_Data !== null
            ? (item3.Created_Data = dayjs(item3.Created_Data + 86400000).format(
                "YYYY-MM-DD"
              ))
            : "";
          parseInt(item.id) === item3.nameID ? item.content.push(item3) : "";
        });
      });
      res.json({
        data: row,
        success: true,
      });
    });
  });
});
//获取新闻公告接口——————————————————————————————————————————————————————————————————
router.get("/PressReleases", (req, res) => {
  console.log("获取新闻公告");
  let row = [];
  if (!req.query.page && !req.query.size) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查询角色表
  let sql = "";
  if (!req.query.name) {
    sql = `SELECT * from press_releases ORDER BY id desc`;
  } else {
    sql = `SELECT * from press_releases WHERE name LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
      // 转化时间
      item.noticeDate !== null
        ? (item.noticeDate = dayjs(item.noticeDate + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          ))
        : "";
    });
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});
//获取名词解析接口——————————————————————————————————————————————————————————————————
router.get("/WordAnalysis", (req, res) => {
  console.log("获取新闻公告");
  // 查询角色表
  let sql = "";
  if (!req.query.name) {
    sql = `SELECT * from word_analysis ORDER BY id desc`;
  } else {
    sql = `SELECT * from word_analysis WHERE name LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
//获取用户信息——————————————————————————————————————————————————————————————————
router.get("/GetUser", (req, res) => {
  console.log("获取用户信息");
  // 查询角色表
  let sql = "";
  if (!req.query.name) {
    sql = `SELECT * from user ORDER BY id desc`;
  } else {
    sql = `SELECT * from user WHERE name LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    let Res = [];
    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
      item.data !== null
        ? (item.data = dayjs(item.data + 86400000).format("YYYY-MM-DD"))
        : "";
      Res.push({
        id: item.id,
        name: item.name,
        Account: item.Account,
        password: item.password,
        qq: item.qq,
        data: item.data,
        disable: item.disable,
        Created_By: item.Created_By,
        Created_Data: item.Created_Data,
        Created_ID: item.Created_ID,
        manage: item.manage,
      });
    });
    res.json({
      data: Res,
      success: true,
    });
  });
});
// 判断是否有权限进入用户管理——————————————————————————————————————————————————————————————————
router.get("/IFUser", (req, res) => {
  console.log("判断是否有权限进入用户管理");
  // 查询角色表
  let sql = `SELECT * from user WHERE id=${req.auth.id} ORDER BY id desc`;
  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    console.log(result);
    if (result.length === 0) {
      res.json({
        msg: "未知错误",
        success: false,
      });
    } else {
      if (result[0].manage === 0 && result[0].disable !== 1) {
        res.json({
          success: true,
        });
      } else {
        res.json({
          msg: "你的账号没有权限",
          success: false,
        });
      }
    }
  });
});
//获取神代盘接口——————————————————————————————————————————————————————————————————
router.get("/GodGeneration", (req, res) => {
  console.log("获取神代盘");
  // 查询角色表
  let sql = "";
  if (!req.query.name) {
    sql = `SELECT * from god_generation ORDER BY id desc`;
  } else {
    sql = `SELECT * from god_generation WHERE name LIKE '%${req.query.name}%' OR color LIKE '%${req.query.name}%' OR description LIKE '%${req.query.name}%' ORDER BY id desc `;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看称号系列枚举接口——————————————————————————————————————————————————————————————————
router.get("/TitleSeries", (req, res) => {
  console.log("查看称号系列枚举");
  // 查询角色表
  let sql = `SELECT * from enum WHERE classify="称号系列" AND level='1'`;
  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
//获取称号一览接口——————————————————————————————————————————————————————————————————
router.get("/TitleEdit", (req, res) => {
  console.log("获取称号一览");
  let row = [];
  // 查询角色表
  let sql = "";
  if (!req.query.page && !req.query.size) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  if (!req.query.name) {
    sql = `SELECT * from title_edit ORDER BY id desc`;
  } else {
    sql = `SELECT * from title_edit WHERE name LIKE '%${req.query.name}%' OR state LIKE '%${req.query.name}%' OR description LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});
// 查询封灭战修改接口——————————————————————————————————————————————————————————————————————
router.get("/ModifiedWarOfAnnihilation", (req, res) => {
  console.log("查询封灭战修改接口");
  // 查询幻灯镜表
  let sql = "";
  if (req.query.state === "审核库") {
    // 查询审核库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from war_of_annihilation WHERE  Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * from war_of_annihilation WHERE name LIKE '%${req.query.Search}%' AND Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc`);
  } else if (req.query.state === "正式库") {
    // 查询正式库
    req.query.Search === undefined || req.query.Search === ""
      ? (sql = `SELECT * from war_of_annihilation WHERE  Agree=1 ORDER BY id desc`)
      : (sql = `SELECT * from war_of_annihilation WHERE name LIKE '%${req.query.Search}%'AND Agree=1 ORDER BY id desc`);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
      item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
      item.Strategy = JSON.parse(item.Strategy);
      item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
      item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
      item.Marketplace_Redemption3 = JSON.parse(item.Marketplace_Redemption3);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item.Created_Data !== null
        ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看详细封灭战接口——————————————————————————————————————————————————————————————————
router.get("/WarOfAnnihilationget", (req, res) => {
  console.log("查看详细封灭战接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "" ||
    req.query.state === undefined ||
    req.query.id === "" ||
    req.query.id === undefined
  ) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    if (req.query.state === "审核库") {
      sql = `SELECT * from war_of_annihilation WHERE id=${req.query.id} AND Agree=0`;
    } else if (req.query.state === "正式库") {
      sql = `SELECT * from war_of_annihilation WHERE id=${req.query.id} AND Agree=1`;
    }
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 调伏战转化
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Marketplace_Redemption3 = JSON.parse(item.Marketplace_Redemption3);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item.Created_Data !== null
          ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
          : "";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查询封灭战审核接口——————————————————————————————————————————————————————————————————
router.get("/WarOfAnnihilation", (req, res) => {
  console.log("查询封灭战审核接口");
  // 查询角色审核表
  let sql = "";
  if (req.query.state === undefined) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else if (req.query.state === "false") {
    // 全部为审核数据
    sql = "SELECT * FROM war_of_annihilation WHERE Agree=0 ORDER BY id desc ";
  } else if (req.query.state === "true") {
    // 只查找自己的审核数据
    //  sift为0查找审核库增加字段的数据
    req.query.sift === "0"
      ? (sql = `SELECT * from  war_of_annihilation WHERE Created_ID=${req.auth.id}  AND Agree=0 ORDER BY id desc`)
      : (sql = `SELECT * FROM war_of_annihilation WHERE Created_ID=${req.auth.id} AND Agree=0 ORDER BY id desc `);
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    //将一些数据转化为js数据
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
      item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
      item.Strategy = JSON.parse(item.Strategy);
      item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
      item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
      item.Marketplace_Redemption3 = JSON.parse(item.Marketplace_Redemption3);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item.Created_Data !== null
        ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
        : "";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
//获取视频攻略审核接口——————————————————————————————————————————————————————————————————
router.get("/VideoStrategy", (req, res) => {
  console.log("获取视频攻略审核接口");
  let row = [];
  if (!req.query.page && !req.query.size) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查询角色表
  let sql = "";
  if (!req.query.name&& !req.query.state) {
    sql = `SELECT * from capture ORDER BY id desc`;
  } else if(req.query.name && !req.query.state){
    sql = `SELECT * from capture WHERE name LIKE '%${req.query.name}%' OR Created_By LIKE '%${req.query.name}%' ORDER BY id desc`;
  }else if(!req.query.name && req.query.state){
    sql = `SELECT * from capture WHERE Agree=${req.query.state} ORDER BY id desc`;
  }else {
    sql = `SELECT * from capture WHERE Agree=${req.query.state} AND name LIKE '%${req.query.name}%' OR Agree=${req.query.state} AND Created_By LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
        // 转化标签数据
        item.tab = JSON.parse(item.tab);
    });
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});
module.exports = router;
